package com.module.retrofit.interceptor;

import com.module.retrofit.utils.HeaderUtils;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 头部拦截器-添加头
 * Created by dell on 2017/2/22
 */

public class HearderInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request()
                .newBuilder();
        Map<String, String> headers = HeaderUtils.getHeaders();
        if (headers != null) {
            Set<String> keys = headers.keySet();
            for (String headerKey : keys) {
                builder.addHeader(headerKey, Objects.requireNonNull(headers.get(headerKey))).build();
            }
        }
        return chain.proceed(builder.build());
    }
}
